Pelican Blog工具特点,静态页面生成速度快,维护简单; 下载最新版 Pelican https://github.com/getpelican/pelican-themes/archive/refs/heads/master.zip. 另外本地应预先安装好Python3.0以上版本,支持Pelican运行; 安装方式参见文档:https://docs.getpelican.com/en/latest/.
Pelican简单设置
Article与Page区别 Article:Blog内容,指具体写五Blog文章内容 Page:通常指固定不变的内容页面,比较个人说明内容; ` 元数据: 指用于定义博客,进行搜索引擎优化的设置,如Blog的基本信息。 可用Markdown语言来编辑Blog内容,格式如下: 多个标签或作者,可以用逗号做分割 多个作者使用authers 仅一个作者使用auther modified值是最后编辑文章的时间,默认和date相同。可以通过modified进行RSS推送,可在刚写文章的时候RSS已经推送了一遍,后来进行了修改,就可以通过modified进行再次推送。
Title:
Date: 2021-07-12 14:25
Modified: 2021-07-15 19:30
Category: Tech
Tags: Python,Blog,
Slug: Howto-supet-blog-with-Python-and-Github
Author: 工长
Blog的Article页面内容部分
页面 在content下面创建了目录pages,该目录下所有的文件都将被生成静态页面,比如About和Contact页面。
可以通过设置DISPLAY_PAGES_ON_MENU来控制页面是否在导航栏显示,默认情况是True
如果希望页面不在导航条上显示,可以在页面的元数据里面添加:status: hidden属性,例如对于404页面设置为不可见,因为404静态页面通常是不希望在导航条上面显示的。
引用内部Article的链接 可以通过content的目录结构在不同的文章之间进行链接,现实文章的引用操作。 引用content里面的内容语法是:
{filename}path/to/file # {fielname}是必填内容,可以同时支持Linux和windows
markdonw格式下样例如下:
[a link relative to the current file]({filename}category/article1.rst)
[a link relative to the content root]({filename}/category/article1.rst)
链接静态文件 需要引用到的静态资源,比如图片,PDF文件等,需要在pelicanconf.py中配置STATIC_PATHS属性,Pelican默认设置content下的images为图片资源,其他的资源需要进行配置。 创建Article页面时,Pelican会将图片复制到output/images/目录下,将pdf复制到output/pdfs/目录下,同时将链接适配到Article文件中
STATIC_PATHS = ['images', 'pdfs']
发布草稿 对部分Article希望作为草稿先发布出来看下效果,不让Article在首页和分类列表里面显示出来,可以在文章中添加Status: draft属性。修改文章的配置为Status: published将文章正式发布出来。
如果将所有刚写好的文章都作为草搞件发布的话,可以在配置文件中配置DEFAULT_METADATA 属性
DEFAULT_METADATA = {
'status': 'draft',
}
文章在发布的时候默认均为草稿,通过修改文章的配置为Status: published将文章正式发布。
Pelican Blog 内容自动发布Github
Github上发布,后续维护成本可忽略不计;个人建立的VPS中发布,如仅针对一人Blog,显然成本占用的维护精力比较大,综合考虑通过Github发布是最优选择。
建立两个repo 1 私有repo,用于保存每次新添加的源文件 2 公有repo,对外提供内容的publish服务,可绑定域名对接访问; 3 私有repo中建立Action,通过Push触发,将指定内容复制到公有repo中,完成自动发布;
本机新加入或编辑Article/Post的内容后,通过使用git发布:
$ pelican content # 更新content内容
$ git add . # git添加
$ git commit -am 'update' # 加入注释内容
$ git push # 向私有repo中Push更新的内容
$ 根据系统提示,录入key完成更新
自动发布,通过Action自动完成从内容源与发布版本的文件传输,约3-5分钟后线上内容完成更新,以下样例工作正常,可根据实际变化调整;
# This is a basic workflow to help you get started with Actions
name: CI-Publish Blog
# Controls when the workflow will run
on:
# Triggers the workflow on push or pull request events but only for the main branch
push:
branches: [ main ]
pull_request:
branches: [ main ]
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
# Secodn job called "publish"
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
submodules: true
#- name: Set up Python
# uses: actions/setup-python@v1
# with:
# python-version: '3.8'
#- name: Insall dependencies
# run: pip install -r requirements.txt
- name: Pushes to another repository
uses: cpina/github-action-push-to-another-repository@main # 关键点,自用Github提供的大牛们写好的Action
env:
API_TOKEN_GITHUB: ${{ secrets.API_TOKEN_GITHUB }} # API_TOKEN_GITHUB, 在当前做为源Repo中的Setting中设置API_TOKEN_GITHUB,本处将自动引用;注意生成或修改时会录入"Personal access tokens",该token需求提前设置好.
with:
source-directory: 'output'
destination-github-username: 'guthub name XXXXXXX' # github用户名
destination-repository-name: 'namen of repository for publlish' # 目标Repo,即对外发布用的Repo
user-email: XXXXXXXX@XXXXXXXX # github email
commit-message: Post
target-branch: master
API_TOKEN_GITHUB的设置入口如图示;
output目录下加入CNAME文件,内容为定制化的域名(Custom Domain),否则每次push后,会修改为Github repo默认的URL进行访问。
错误处理
pelican content
Traceback (most recent call last):
File "D:\Program Files (x86)\Python\Python37\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "D:\Program Files (x86)\Python\Python37\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "D:\Program Files (x86)\Python\Python37\Scripts\pelican.exe\__main__.py", line 5, in <module>
File "D:\Program Files (x86)\Python\Python37\lib\site-packages\pelican\__init__.py", line 19, in <module>
from pelican.generators import (ArticlesGenerator, # noqa: I100
File "D:\Program Files (x86)\Python\Python37\lib\site-packages\pelican\generators.py", line 17, in <module>
from pelican.readers import Readers
File "D:\Program Files (x86)\Python\Python37\lib\site-packages\pelican\readers.py", line 23, in <module>
from markdown import Markdown
File "D:\Program Files (x86)\Python\Python37\lib\site-packages\markdown\__init__.py", line 29, in <module>
from .core import Markdown, markdown, markdownFromFile # noqa: E402
File "D:\Program Files (x86)\Python\Python37\lib\site-packages\markdown\core.py", line 26, in <module>
from . import util
File "D:\Program Files (x86)\Python\Python37\lib\site-packages\markdown\util.py", line 87, in <module>
INSTALLED_EXTENSIONS = metadata.entry_points().get('markdown.extensions', ())
AttributeError: 'EntryPoints' object has no attribute 'get'
检查运行python环境中importlib-metadata库的版本为6.6.0;其它模块升级后引起的版本不兼容问题,重新安装低版本importlib-metadata即可;尝试了5.0.0版本,兼容问题依旧,更改为importlib-metadata=4.13.0解决。
pip list | findstr "importlib"
importlib-metadata 6.6.0
importlib-resources 5.4.0
更新
pip install importlib-metadata==4.13.0
Links:
- Ref Link: https://github.com/look4wine/pelican-octopress-theme
- Ref Link: https://github.com/getpelican/pelican
- Ref Link: https://github.com/getpelican/pelican-themes